package com.lkm.market2_0.m_imple.steelyardserver.server.sendtask;

import com.lkm.frame.P;
import com.lkm.frame.task2.ITaskExecutor;
import com.lkm.frame.task2.TaskManager;
import com.lkm.hardware_connect.IHardwareControler;
import com.lkm.javatool.CollectionHelp;
import com.lkm.javatool.NumberHelp;
import com.lkm.market2_0.help.UIP;
import com.lkm.market2_0.m_imple.steelyardserver.server.AbsSendWriteTask;
import com.lkm.market2_0.m_imple.steelyardserver.server.ReceiveTask;
import com.lkm.market2_0.m.steelyardserver.GoodsButtonInfoTo;

import java.io.UnsupportedEncodingException;
import java.util.List;



public class SetGoodsButtonTask extends AbsSendWriteTask {

    

    public SetGoodsButtonTask(ITaskExecutor<Object[], Void, Boolean> taskExecutor, TaskManager mTaskManager, IHardwareControler bluetoothSocket, ReceiveTask mReceiveTask,List<GoodsButtonInfoTo> goodsInfoTos) {
        super(taskExecutor, SetGoodsButtonTask.class.getName(), mTaskManager, bluetoothSocket,mReceiveTask);
        setParam(new Object[]{goodsInfoTos});
    }

    @Override
    public void doExecuting(Object[] param) {
        if(P.isDebug()) {
            P.p("=======开始设置菜品按钮");
            if(UIP.isPrint()){
                UIP.p("Send设置菜品按钮：开始",mReceiveTask==null?"":mReceiveTask.steelyardMac);
            }
        }

        List<GoodsButtonInfoTo> goodsInfoTos=(List<GoodsButtonInfoTo>)getParam()[0];
        
        if(CollectionHelp.isEmpty(goodsInfoTos)){
            cancel();
            return;
        }

        super.doExecuting(param);
    }

    @Override
    protected byte onGetRequestCMD(int[] outDataSize) {
        List<GoodsButtonInfoTo> goodsInfoTos=(List<GoodsButtonInfoTo>)getParam()[0];

        
        int dataSize=goodsInfoTos.size()*16;

        outDataSize[0]=dataSize;
        return 0x04;
    }

    @Override
    protected byte[] onGetSendDataItem(int index) {
        List<GoodsButtonInfoTo> goodsInfoTos=(List<GoodsButtonInfoTo>)getParam()[0];
        if(goodsInfoTos==null)return null;

        int dataitemLength=16;


        
        int pageSize = (4096/dataitemLength);

        int startIndex=pageSize*index;
        int endIndex=pageSize*(index+1);
        if(startIndex>=goodsInfoTos.size()){
            return null;
        }
        endIndex=Math.min(goodsInfoTos.size(),endIndex);

        int curPageSize=endIndex-startIndex;

        byte [] data=new byte[curPageSize*dataitemLength];

        for (int i = startIndex; i < endIndex; i++) {

            GoodsButtonInfoTo goodsInfoTo= goodsInfoTos.get(i);
            String name = holdCharWidth(12,goodsInfoTo.getName());

            int price = ((int) (goodsInfoTo.getPrice()*1000))/10;

            
            byte[] nameBytes=null;
            try {
                nameBytes=name.getBytes("gbk");
            } catch (UnsupportedEncodingException e) {
                nameBytes=new byte[12];
                e.printStackTrace();
            }
            
            byte[] priceBytes= NumberHelp.intToBytesDesc(price);

            int start=(i-startIndex)*dataitemLength;

            for (int j = 0; j < 12; j++) {
                
                data[start + j] = j >= nameBytes.length ? 32 : nameBytes[j];
            }
            data[start+12]=priceBytes[0];
            data[start+12+1]=priceBytes[1];
            data[start+12+2]=priceBytes[2];
            data[start+12+3]=priceBytes[3];

        }

        return data;
    }

    @Override
    public void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    public void onCancel() {
        super.onCancel();
        if (P.isDebug()) {
            P.p("=======开始设置菜品按钮取消了");
            if (UIP.isPrint()) {
                UIP.p("Send设置菜品按钮：取消",mReceiveTask==null?"":mReceiveTask.steelyardMac);

            }
        }
    }

    @Override
    public void onFinish(Boolean aBoolean) {
        super.onFinish(aBoolean);

        if (P.isDebug()) {
            P.p("======设置菜品按钮是否成功："+aBoolean);;
            if (UIP.isPrint()) {
                UIP.p("Send设置菜品按钮：成功？"+aBoolean,mReceiveTask==null?"":mReceiveTask.steelyardMac);

            }
        }
    }
}